//
//  DictionaryViewController.m
//  Dictionary
//
//  Created by Lion User on 08/07/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DictionaryViewController.h"

@interface DictionaryViewController ()

@end

@implementation DictionaryViewController
@synthesize theTitle;
@synthesize theSubtitle;
@synthesize theLatitude;
@synthesize theLongitude;
@synthesize theURL;
@synthesize searchText;
@synthesize buildingpicker;

- (IBAction)displayInfo:(id)sender {
    NSString *searchName=self.searchText.text;
    [self.searchText resignFirstResponder];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"nameList" ofType:@"ls"];
    NSString* content = [NSString stringWithContentsOfFile:path
                        encoding:NSUTF8StringEncoding
                         error:NULL];  
//  NSLog(@"%@",content); 
    NSArray *lines = [content componentsSeparatedByString:@"\n"];
    NSMutableDictionary *pictureInfo=[[NSMutableDictionary alloc]init];
    for (NSString *line in lines)
    {
//     NSLog(@"%@",line);     
       NSArray *locationInfo=[line componentsSeparatedByString:@","];
        if([locationInfo count]>1){
           [pictureInfo setObject:[locationInfo objectAtIndex:1] forKey:[locationInfo objectAtIndex:0]]; 
        }
    }
//    for (id key in pictureInfo) {
//        NSLog(@"key: %@, value: %@", key, [pictureInfo objectForKey:key]);   
//    }
    
    NSString *infoOfLocation=[pictureInfo objectForKey:searchName];
    NSLog(@"key: %@, value: %@", searchName, infoOfLocation); 
    if (infoOfLocation) {
        NSArray *infoArray=[infoOfLocation componentsSeparatedByString:@":"];
        self.theTitle.text=[infoArray objectAtIndex:0];
        NSLog(@"the Title=%@",self.theTitle.text);
        self.theSubtitle.text=[infoArray objectAtIndex:1];
        self.theLatitude.text=[infoArray objectAtIndex:2];
        self.theLongitude.text=[infoArray objectAtIndex:3];
    }
    else {
        self.searchText.text=@"not_found";
    }

}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)viewDidUnload {
    [self setSearchText:nil];
    [self setBuildingpicker:nil];
    [super viewDidUnload];
}
@end
